home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / unarj_st.lzh / UNARJ_ST / UNARJ230.EXE / UNARJ.C < prev    next >
C/C++ Source or Header  |  1991-12-04  |  23KB  |  1,009 lines

  1. /* UNARJ.C, UNARJ, R JUNG, 12/03/91
  2.  * Main Extractor routine
  3.  * Copyright (c) 1991 by Robert K Jung.  All rights reserved.
  4.  *
  5.  *   This code may be freely used in programs that are NOT ARJ archivers
  6.  *   (both compress and extract ARJ archives).
  7.  *
  8.  *   If you wish to distribute a modified version of this program, you
  9.  *   MUST indicate that it is a modified version both in the program and
  10.  *   source code.
  11.  *
  12.  *   If you modify this program, I would appreciate a copy of the new
  13.  *   source code.  I am holding the copyright on the source code, so
  14.  *   please do not delete my name from the program files or from the
  15.  *   documentation.
  16.  *
  17.  *   I wish to give credit to Haruhiko Okumura for providing the
  18.  *   basic ideas for ARJ and UNARJ in his program AR.  Please note
  19.  *   that UNARJ is significantly different from AR from an archive
  20.  *   structural point of view.
  21.  *
  22.  * Modification history:
  23.  * Date      Programmer  Description of modification.
  24.  * 04/05/91  R. Jung     Rewrote code.
  25.  * 04/23/91  M. Adler    Portabilized.
  26.  * 04/29/91  R. Jung     Added l command.  Removed 16 bit dependency in
  27.  *                       fillbuf().
  28.  * 05/19/91  R. Jung     Fixed extended header skipping code.
  29.  * 05/25/91  R. Jung     Improved find_header().
  30.  * 06/03/91  R. Jung     Changed arguments in get_mode_str() and
  31.  *                       set_ftime_mode().
  32.  * 06/19/81  R. Jung     Added two more %c in printf() in list_arc().
  33.  * 07/07/91  R. Jung     Added default_case_path() to extract().
  34.  *                       Added strlower().
  35.  * 07/20/91  R. Jung     Changed uint ratio() to static uint ratio().
  36.  * 07/21/91  R. Jung     Added #ifdef VMS.
  37.  * 08/28/91  R. Jung     Changed M_DIFFHOST message.
  38.  * 08/31/91  R. Jung     Added changes to support MAC THINK_C compiler
  39.  *                       per Eric Larson.
  40.  * 10/07/91  R. Jung     Added missing ; to THINK_C additions.
  41.  * 11/11/91  R. Jung     Added host_os test to fwrite_txt_crc().
  42.  * 11/24/91  R. Jung     Added more error_count processing.
  43.  * 12/03/91  R. Jung     Added backup file processing.
  44.  *
  45.  */
  46.  
  47. #include "unarj.h"
  48.  
  49. #ifdef MODERN
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <ctype.h>
  53. #else /* !MODERN */
  54. extern void free();
  55. extern void exit();
  56. extern char *strcat();
  57. extern char *strcpy();
  58. extern char *strncpy();
  59. extern char *strchr();
  60. extern char *strrchr();
  61. extern int strlen();
  62. extern int strcmp();
  63. #ifdef VMS
  64. #include <ssdef.h>
  65. #define EXIT_FAILURE SS$_ABORT
  66. #define EXIT_SUCCESS SS$_NORMAL
  67. #else
  68. #define EXIT_FAILURE (1)
  69. #define EXIT_SUCCESS (0)
  70. #endif
  71. #define toupper(c)   ((c)>='a'&&(c)<='z'?(c)-('a'-'A'):(c))
  72. #define tolower(c)   ((c)>='A'&&(c)<='Z'?(c)+('a'-'A'):(c))
  73. #endif /* ?MODERN */
  74.  
  75. #ifdef THINK_C
  76. #include <console.h>
  77. #endif
  78.  
  79. /* Global variables */
  80.  
  81. UCRC   crc;
  82. FILE   *arcfile;
  83. FILE   *outfile;
  84. ushort bitbuf;
  85. long   compsize;
  86. long   origsize;
  87. uchar  subbitbuf;
  88. uchar  header[HEADERSIZE_MAX];
  89. char   arc_name[FNAME_MAX];
  90. int    command;
  91. int    bitcount;
  92. int    file_type;
  93. int    no_output;
  94. int    error_count;
  95.  
  96. /* Messages */
  97.  
  98. static char *M_USAGE  [] =
  99. {
  100. "Usage:  UNARJ archive[.arj]    (list archive)\n",
  101. "        UNARJ e archive        (extract archive)\n",
  102. "        UNARJ l archive        (list archive)\n",
  103. "        UNARJ t archive        (test archive)\n",
  104. "        UNARJ x archive        (extract with pathnames)\n",
  105. "\n",
  106. "This is an ARJ demonstration program and ** IS NOT OPTIMIZED ** for speed.\n",
  107. "You may freely use, copy and distribute this program, provided that no fee\n",
  108. "is charged for such use, copying or distribution, and it is distributed\n",
  109. "ONLY in its original unmodified state.  UNARJ is provided as is without\n",
  110. "warranty of any kind, express or implied, including but not limited to\n",
  111. "the implied warranties of merchantability and fitness for a particular\n",
  112. "purpose.  Refer to UNARJ.DOC for more warranty information.  If you find\n",
  113. "UNARJ of value, a gift of $10 or any amount would greatly appreciated.\n",
  114. "\n",
  115. "Robert K Jung                   Internet address :  robjung@world.std.com\n",
  116. "2606 Village Road West          CompuServe userid:  72077,445\n",
  117. "Norwood, Massachusetts 02062\n",
  118. "USA\n",
  119. NULL
  120. };
  121.  
  122. char M_VERSION [] = "UNARJ (Demo version) 2.30 Copyright (c) 1991 Robert K Jung\n\n";
  123.  
  124. char M_ARCDATE [] = "Archive date      : %s\n";
  125. char M_BADCOMND[] = "Bad UNARJ command: %s";
  126. char M_BADCOMNT[] = "Invalid comment header";
  127. char M_BADHEADR[] = "Bad header";
  128. char M_BADTABLE[] = "Bad Huffman code";
  129. char M_CANTOPEN[] = "Can't open %s";
  130. char M_CANTREAD[] = "Can't read file or unexpected end of file";
  131. char M_CANTWRIT[] = "Can't write file. Disk full?";
  132. char M_CRCERROR[] = "CRC error!\n";
  133. char M_CRCOK   [] = "CRC OK\n";
  134. char M_DIFFHOST[] = "  Binary file!";
  135. char M_ENCRYPT [] = "File is password encrypted, ";
  136. char M_ERRORCNT[] = "%sFound %5d error(s)!";
  137. char M_EXTRACT [] = "Extracting %-25s";
  138. char M_FEXISTS [] = "%-25s exists, ";
  139. char M_HEADRCRC[] = "Header CRC error!";
  140. char M_NBRFILES[] = "%5d file(s)\n";
  141. char M_NOMEMORY[] = "Out of memory";
  142. char M_NOTARJ  [] = "%s is not an ARJ archive";
  143. char M_PROCARC [] = "Processing archive: %s\n";
  144. char M_SKIPPED [] = "Skipped %s\n";
  145. char M_SUFFIX  [] = ARJ_SUFFIX;
  146. char M_TESTING [] = "Testing    %-25s";
  147. char M_UNKNMETH[] = "Unsupported method: %d, ";
  148. char M_UNKNTYPE[] = "Unsupported file type: %d, ";
  149. char M_UNKNVERS[] = "Unsupported version: %d, ";
  150.  
  151. #define get_crc()       get_longword()
  152. #define fget_crc(f)     fget_longword(f)
  153.  
  154. #define setup_get(PTR)  (get_ptr = (PTR))
  155. #define get_byte()      ((uchar)(*get_ptr++ & 0xff))
  156.  
  157. #define BUFFERSIZE      4096
  158.  
  159. #define ASCII_MASK      0x7F
  160.  
  161. #define CRCPOLY         0xEDB88320L
  162.  
  163. #define UPDATE_CRC(r,c) r=crctable[((uchar)(r)^(uchar)(c))&0xff]^(r>>CHAR_BIT)
  164.  
  165. /* Local functions */
  166.  
  167. #ifdef MODERN
  168. static void  make_crctable(void);
  169. static void  crc_buf(char *str, int len);
  170. static void  strparity(uchar *p);
  171. static FILE  *fopen_msg(char *name, char *mode);
  172. static int   fget_byte(FILE *f);
  173. static uint  fget_word(FILE *f);
  174. static ulong fget_longword(FILE *f);
  175. static void  fread_crc(uchar *p, int n, FILE *f);
  176. static void  decode_path(char *name);
  177. static void  get_date_str(char *str, ulong tstamp);
  178. static int   parse_path(char *pathname, char *path, char *entry);
  179. static void  strncopy(char *to, char *from, int len);
  180. static uint  get_word(void);
  181. static ulong get_longword(void);
  182. static long  find_header(FILE *fd);
  183. static int   read_header(int first, FILE *fd, char *name);
  184. static void  skip(void);
  185. static void  unstore(void);
  186. static int   check_flags(void);
  187. static int   extract(void);
  188. static int   test(void);
  189. static uint  ratio(long a, long b);
  190. static void  list_start(void);
  191. static void  list_arc(int count);
  192. static void  execute_cmd(void);
  193. static void  help(void);
  194. #endif /* MODERN */
  195.  
  196. /* Local variables */
  197.  
  198. static char   filename[FNAME_MAX];
  199. static char   comment[COMMENT_MAX];
  200. static char   *hdr_filename;
  201. static char   *hdr_comment;
  202.  
  203. static ushort headersize;
  204. static uchar  first_hdr_size;
  205. static uchar  arj_nbr;
  206. static uchar  arj_x_nbr;
  207. static uchar  host_os;
  208. static uchar  arj_flags;
  209. static short  method;
  210. static uint   file_mode;
  211. static ulong  time_stamp;
  212. static short  entry_pos;
  213. static ushort host_data;
  214. static uchar  *get_ptr;
  215. static UCRC   file_crc;
  216. static UCRC   header_crc;
  217.  
  218. static long   first_hdr_pos;
  219. static long   torigsize;
  220. static long   tcompsize;
  221.  
  222. static int    clock_inx;
  223.  
  224. static char   *writemode[2]  = { "wb",  "w" };
  225.  
  226. static UCRC   crctable[UCHAR_MAX + 1];
  227.  
  228. /* Functions */
  229.  
  230. static void
  231. make_crctable()
  232. {
  233.     uint i, j;
  234.     UCRC r;
  235.  
  236.     for (i = 0; i <= UCHAR_MAX; i++)
  237.     {
  238.         r = i;
  239.         for (j = CHAR_BIT; j > 0; j--)
  240.         {
  241.             if (r & 1)
  242.                 r = (r >> 1) ^ CRCPOLY;
  243.             else
  244.                 r >>= 1;
  245.         }
  246.         crctable[i] = r;
  247.     }
  248. }
  249.  
  250. static void
  251. crc_buf(str, len)
  252. char *str;
  253. int  len;
  254. {
  255.     while (len--)
  256.         UPDATE_CRC(crc, *str++);
  257. }
  258.  
  259. void
  260. disp_clock()
  261. {
  262.     static char clock_str[4] = { '|', '/', '-', '\\' };
  263.  
  264.     printf("(%c)\b\b\b", clock_str[clock_inx]);
  265.     clock_inx = (clock_inx + 1) & 0x03;
  266. }
  267.  
  268. void
  269. error(fmt, arg)
  270. char *fmt;
  271. char *arg;
  272. {
  273.     putc('\n', stdout);
  274.     printf(fmt, arg, error_count);
  275.     putc('\n', stdout);
  276.     exit(EXIT_FAIL